//
//  SideslipMenuViewController.m
//  YiShangPin
//
//  Created by 衣尚品1 on 15/3/9.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "SideslipMenuViewController.h"
#import "AppDelegate.h"
#import "SMTableViewCell.h"
#import "Collect.h"

@interface SideslipMenuViewController (){
    
    NSMutableArray *ListImageArray;
    NSMutableArray *ListLabelArray;
    AppDelegate *delegate;
    YRSideViewController *YRController;
}


@end

@implementation SideslipMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    YRController = delegate.sideViewController;

    //设置removeButton的圆角
    self.removeButton.layer.cornerRadius = 3;
    //隐藏removeView
    self.removeView.hidden = YES;
    
    //注册显示和隐藏remove
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showRemoveView) name:@"showRemoveView" object:nil];
    //该通知在侧滑隐藏的第三方库中
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideRemoveView) name:@"hideRemoveView" object:nil];
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{


    if (delegate.CollectArray.count>=1) {
        ListImageArray = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:@"fushi%d.jpg",[delegate.CollectArray[delegate.CollectArray.count-1] integerValue]-1000], nil] ;
    }
    else
    {
    ListImageArray = [[NSMutableArray alloc] initWithObjects:@"Empty.jpeg", nil] ;
    
    }
    
    ListLabelArray = [[NSMutableArray alloc] initWithObjects:@"Miscellaneous", nil];
    if ([_CreateWishlist isEqualToString:@"Create Wishlist"]) {
        [ListLabelArray insertObject:_CreateWishlist atIndex:0];
        [ListImageArray insertObject:@"ADD.png" atIndex:0];
    }

    [self.MyTableView reloadData];
}

-(void)viewWillDisappear:(BOOL)animated{

    [ListLabelArray removeObjectAtIndex:0];
    _CreateWishlist = @"NULL";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return ListLabelArray.count;
    
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//        UINib *nib = [UINib nibWithNibName:@"SMTableViewCell" bundle:nil];
//        [self.MyTableView registerNib:nib forCellReuseIdentifier:@"Cell"];
    
        SMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
        [cell.ProButton setImage:[UIImage imageNamed:ListImageArray[indexPath.row]] forState:0];

    NSLog(@"%@",ListImageArray[indexPath.row]);
        cell.ProLalel.text =[NSString stringWithFormat:@"  %@", ListLabelArray[indexPath.row]];
    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
//    
//    cell.textLabel.text = ListImageArray[indexPath.row];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 95;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)ClickPic:(id)sender {
    if ([ListImageArray[0] isEqualToString:@"Empty.jpeg"]) {
        //如果为默认状态，则提示“用户没有收藏商品”
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Hint" message:@"You didn't collect any items." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else{
        
    Collect *collect = [self.storyboard instantiateViewControllerWithIdentifier:@"Collect"];
        NSLog(@"%@",[delegate CurrentViewController]);
    [[delegate CurrentViewController] pushViewController:collect animated:YES];
    [YRController hideSideViewController:YES];
    }
}
- (IBAction)clickRemoveButton:(id)sender {
    for (int i=0; i < delegate.CollectArray.count; i++) {
        if ([delegate.CollectArray[i] intValue] == delegate.collectTag) {
            [delegate.CollectArray removeObjectAtIndex:i];
        }
    }
    [YRController hideSideViewController:YES];
    
    [delegate.waterfall.Collect setTitle:@"  Save" forState:0];
    [delegate.waterfall.Collect setImage:[UIImage imageNamed:@"xiaoaixin.png"] forState:0];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeButton" object:nil];
}

#pragma mark - 隐藏和显示removeView

- (void)showRemoveView{
    self.removeView.hidden = NO;
}

- (void)hideRemoveView{
    //延迟0.5秒执行隐藏
    [self performSelector:@selector(hideRemoveView1) withObject:nil afterDelay:0.5f];
}

- (void)hideRemoveView1{
    self.removeView.hidden = YES;
}

@end
